﻿using UnityEngine;
using UnityEngine.Animations;

namespace UFrame.SMB
{
    /// <summary>
    /// animator进入状态发送对应事件
    /// </summary>
    public class EventSMB : StateMachineBehaviour
    {
        //public GameObject go;
        public string enterArg;
        public string exitArg;
        public string updateOnceArg;
        public bool isEnterEvent = false;
        public bool isExitEvent = false;
        public bool isUpdateEvent = false;
        public bool onceEvent = false;
        private bool isRecordOnce = false;

        public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex,
            AnimatorControllerPlayable controller)
        {
            if (isEnterEvent)
            {
                var a = animator.GetComponentInParent<IAnimaStateEvent>();
                if (a != null)
                    a.OnAnimaStateEvent(enterArg);
            }

            isRecordOnce = false;
        }


        public override void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex,
            AnimatorControllerPlayable controller)
        {
            if (isExitEvent)
            {
                var a = animator.GetComponentInParent<IAnimaStateEvent>();
                if (a != null)
                    a.OnAnimaStateEvent(exitArg);
            }

            isRecordOnce = false;
        }

        public override void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex,
            AnimatorControllerPlayable controller)
        {
            if (isUpdateEvent)
            {
                if (stateInfo.normalizedTime > 1)
                {
                    if (isRecordOnce)
                        return;
                    isRecordOnce = true;
                    //Debug.Log("stateInfo.normalizedTime " + stateInfo.normalizedTime);
                    var a = animator.GetComponentInParent<IAnimaStateEvent>();
                    if (a != null)
                        a.OnAnimaStateEvent(updateOnceArg);
                    
                }
                
                
            }
        }
    }
}